package info.monkeyws.AutoRinger;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Resources;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.preference.Preference.OnPreferenceClickListener;
import android.widget.Toast;

public class Settings extends PreferenceActivity {
	public static final int PERCENT_LOW_BATTERY = 15; //Indicating battery low when below this percentage
	public static final int INDEX_MISSED_CALL = 50; //Volume index added for missed call
	public static final int TIME_INTERVAL_WHEN_BUSY = 1000; //In MILLISECONDS! Time for the next volume sampling when busy
	public static final int TIME_INTERVAL_LOW_BATTERY = 30; //In minutes!
	
    //Below are settings of the application
	public static final int DEFAULT_MIC_SAMPLE_LENGTH = 100000;
    public static final int DEFAULT_QUIET_THRESHOLD = 30;
    public static final int DEFAULT_MIN_THRESHOLD = 50; //above quiet_threshold
    public static final int DEFAULT_MAX_THRESHOLD = 400; //above quiet_threshold
	public static final boolean  DEFAULT_ENABLE_SLEEP_TIME = false;
	public static final int DEFAULT_SLEEP_BEGIN_HOUR = 22;
	public static final int DEFAULT_SLEEP_BEGIN_MINUTE = 0;
	public static final int DEFAULT_SLEEP_END_HOUR = 7;
	public static final int DEFAULT_SLEEP_END_MINUTE = 0;
	public static final boolean  DEFAULT_ALWAYS_RING_VIP = true;
	public static final boolean  DEFAULT_AVOID_MISSING_CALLS = true;
	public static final boolean  DEFAULT_AUTO_TIME_INTERVAL = true;
	public static final int  DEFAULT_TIME_INTERVAL = 6;
	
	private Resources myResources;
	private SharedPreferences pref;
	private Editor editor;
	private Preference pref_service_running;
	private Preference pref_about_application;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);                
        
        myResources = getResources();
        pref = PreferenceManager.getDefaultSharedPreferences(this);
        editor = pref.edit();
        
        if (pref.getBoolean("first_time_run", true) == true){
        	 AlertDialog dialog = new AlertDialog.Builder(this).create();        	 
        	 CharSequence dialog_msg = myResources.getText(R.string.msg_first_time_run);
        	 CharSequence txt_ok = myResources.getText(R.string.txt_ok);
        	 dialog.setMessage(dialog_msg);
        	 dialog.setButton(DialogInterface.BUTTON_NEUTRAL, txt_ok,
       			 new DialogInterface.OnClickListener() {
        			 public void onClick(DialogInterface dialog, int which) {
        				 //Do nothing
        			 }
     			 });
   			 dialog.show();


        	 editor.putBoolean("first_time_run", false);        	 
        	 editor.putBoolean("service_running", false);
        	 editor.putInt("quiet_threshold", DEFAULT_QUIET_THRESHOLD);
        	 editor.putBoolean("enable_sleep_time", DEFAULT_ENABLE_SLEEP_TIME);
        	 editor.putInt("sleep_begin_hour", DEFAULT_SLEEP_BEGIN_HOUR);
        	 editor.putInt("sleep_begin_minute", DEFAULT_SLEEP_BEGIN_MINUTE);
        	 editor.putInt("sleep_end_hour", DEFAULT_SLEEP_END_HOUR);
        	 editor.putInt("sleep_end_minute", DEFAULT_SLEEP_END_MINUTE);
        	 editor.putBoolean("always_ring_vip", DEFAULT_ALWAYS_RING_VIP);
        	 editor.putBoolean("avoid_missing_calls", DEFAULT_AVOID_MISSING_CALLS);
        	 editor.putBoolean("auto_time_interval", DEFAULT_AUTO_TIME_INTERVAL);
        	 editor.putInt("time_interval", DEFAULT_TIME_INTERVAL);
        	 editor.commit();
        }
        
        //Data should be prepared before showing the UI.
        addPreferencesFromResource(R.xml.settings);
                
        pref_service_running = findPreference("service_running");
        //changeLable();
        pref_service_running.setOnPreferenceClickListener(new OnPreferenceClickListener(){

			@Override
			public boolean onPreferenceClick(Preference arg0) {
				// TODO Auto-generated method stub
				
				String key = arg0.getKey();
				Boolean running = pref.getBoolean(key, false);
				if (running == false){	//NEED TO IMPROVE: There should a better way to see if a service is running
					Toast.makeText(Settings.this, R.string.msg_service_started, Toast.LENGTH_SHORT).show();
					startService(new Intent(Settings.this, AutoRingerService.class));					
				}else{					
					Toast.makeText(Settings.this, R.string.msg_service_stopped, Toast.LENGTH_SHORT).show();
					stopService(new Intent(Settings.this, AutoRingerService.class));
					editor.putBoolean("service_running", false);
					editor.commit();
				}
				
				//changeLable();
				
				return false;
			}
        	
       });
        
       pref_about_application = findPreference("pref_about_application");
       pref_about_application.setOnPreferenceClickListener(new OnPreferenceClickListener(){
			@Override
			public boolean onPreferenceClick(Preference arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(Settings.this, AboutApplication.class);
				startActivity(intent);
				
				return false;
			}
        	
       });
                       
    }
	
}
    
 